package com.training.java.redbox.domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class InventoryDAOJDBCImpl extends BaseDAO implements InventoryDAO{

	@Override
	public List<Inventory> getAllInventory() throws DAOException {
		
		List<Inventory> invnetories = new ArrayList<Inventory>();
		
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
	
		try {
			
			connection = getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery("select * from INVENTORY");
			
			while(resultSet.next())
			{
				long id = Long.parseLong(resultSet.getString("ID"));
				long movieId = Long.parseLong(resultSet.getString("MOVIE_ID"));
				int count = resultSet.getInt("COUNT");
				
				Inventory inventory = new Inventory(id,movieId,count);
				invnetories.add(inventory);
				
			}	
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while adding a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
		
		return invnetories;
	}

	@Override
	public void addInventory(Inventory inventory) throws DAOException {
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
	
		try {
			
			connection = getConnection();
			statement = connection.prepareStatement("insert into INVENTORY values(?,?,?)");
			
			//statement.setString(1,inventory.getId());
			//statement.setString(2,inventory.getMovieId());
			
			statement.setLong(1,inventory.getId());
			statement.setLong(2,inventory.getMovieId());
			statement.setInt(3,inventory.getCount());	
			
			int noOfRowsInserted = statement.executeUpdate();

			if (noOfRowsInserted != 1) {
				throw new DAOException("Inserting Inventory failed");
			}
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while adding a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
		
	}

	@Override
	public void update(Inventory inventory) throws DAOException {
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
	
		try {
			
			connection = getConnection();
			statement = connection.prepareStatement("update INVENTORY set MOVIE_ID = ?,COUNT = ? where id = ?");
			
			statement.setLong(3,inventory.getId());
			statement.setLong(1,inventory.getMovieId());
			statement.setInt(2,inventory.getCount());	
			
			int noOfRowsInserted = statement.executeUpdate();

			if (noOfRowsInserted != 1) {
				throw new DAOException("update failed");
			}
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while update a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
		
	}

	@Override
	public void delete(long inventoryId) throws DAOException {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
	
		try {
			
			connection = getConnection();
			statement = connection.prepareStatement("delete from inventory where ID = ? ");
			
			statement.setLong(1,inventoryId);
			
			int noOfRowsInserted = statement.executeUpdate();

			if (noOfRowsInserted != 1) {
				throw new DAOException("delete failed");
			}
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while delete a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
	}

	@Override
	public Inventory findByPrimaryKey(long inventoryId) throws DAOException {
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		Inventory inventory = null;
	
		try {
			
			connection = getConnection();
			statement = connection.prepareStatement("select * from inventory where MOVIE_ID = ? ");
			statement.setLong(1,inventoryId);
			
			resultSet = statement.executeQuery();
			
			if(resultSet.next())
			{
				//long id  = resultSet.getLong("ID");
				
				long id = Long.parseLong(resultSet.getString("ID"));
				long movieId = Long.parseLong(resultSet.getString("MOVIE_ID"));
				//long movieId = resultSet.getLong("MOVIE_ID");
				int count = resultSet.getInt("COUNT");
				
				inventory = new Inventory(id,movieId,count);
				
			}	
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while adding a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
		
		return inventory;
	}
	
	@Override
	public void updateInventory(Inventory inventory) throws DAOException {
		
		System.out.println("working");
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		
	
		try {
			
			connection = getConnection();
			statement = connection.prepareStatement("update inventory set id = ?,count = ? where movie_id = ? ");
			
			int count = inventory.getCount();
			count--;
			statement.setLong(1,inventory.getId());
			statement.setLong(3,inventory.getMovieId());
			statement.setInt(2,count);	
			
			int noOfRowsInserted = statement.executeUpdate();

			if (noOfRowsInserted != 1) {
				throw new DAOException("update failed");
			}
			
		} catch (SQLException e) {
			
			throw new DAOException(e,
					"Error while update a record to database");
		}finally {
			
			closeResources(connection, statement, resultSet);
		}
		
		
	}



}
